/**
 * @fileOverview
 * The worker code of WashizuMahjong.
 * http://code.google.com/p/washizu-mahjong/
 */
/**
 * @namespace
 */
var Dora = new function(){
	/**
	 * To add dora. When a round start, it will has 1 dora. Also, when a player doing kong, it also add new dora.
	 * @see Dora.doraIndicators
	 * @see Dora.doraIndicators
	 * @see Game.startNewRound
	 * @see Player#kongAction
	 */
	this.addDora = function(){
		// set dora
		var index = parseInt(Math.random() * gameTiles.length);
		this.doraIndicators.push(gameTiles[index]);
		ActionMessage(ActionMessage.AddDora, null, gameTiles[index], null, {doratype: "normal Dora"});
		gameTiles.removeByIndex(index);

		// set ura dora
		index = parseInt(Math.random() * gameTiles.length);
		this.uraDoraIndicators.push(gameTiles[index]);
		ActionMessage(ActionMessage.AddDora, null, gameTiles[index], null, {doratype: "Ura Dora"});
		gameTiles.removeByIndex(index);
	}
	/**
	 * 表ドラ／槓ドラ表示牌
	 * @type MahjongTile[]
	 */
	this.doraIndicators = [];
	/**
	 * 裏ドラ／槓ウラ表示牌
	 * @type MahjongTile[]
	 */
	this.uraDoraIndicators = [];
	/**
	 * Clear doras.
	 * @see Game.startNewRound
	 */
	this.clean = function(){
		this.doraIndicators = [];
		this.uraDoraIndicators = [];
	}
	/**
	 * Since the rule of dora is that if dora tile is <span class="unicode">&#126976;</span>, dora is<span class="unicode">&#126977;</span>.
	 * This function is used to get real dora list.
	 * @param {MahjongTile[]}[d=dora] the dora list.
	 * @see Dora.doraIndicators
	 * @see Dora.doraIndicators
	 * @return {MahjongTile[]}	The doras. For example, if dora indicators is 一萬（🀇）, it will return 二萬（🀈）.
	 */
	this.getDoras = function(d){
		d = d || this.doraIndicators;

		var list = [];
		var t="\u6771,\u5357,\u897f,\u5317,\u767d,\u767c,\u4e2d".split(","),
		    chinesetext="\u4e00,\u4e8c,\u4e09,\u56db,\u4e94,\u516d,\u4e03,\u516b,\u4e5d".split(","),
		    typeList=["\u5b57\u724c","\u842c","\u7d22","\u9905"];
		for(var i = 0; i < d.length; i++){
			var nText = d[i].tileName;	//new tile name
			var nType = d[i].tileType;	//new tile type
			var nPicText = d[i].tilePicText;
			var smallestPic;

			if(nType == typeList[0]){
				//字牌
				var tIndex = t.indexOf(nText);
				if(tIndex < 4){
					//東南西北
					smallestPic = 126976;
					nText = t[(tIndex + 1) % 4];
					nPicText = "&#"+(parseInt(smallestPic)+((tIndex + 1) % 4))+";";
				}else if(tIndex < 7){
					//白發中
					smallestPic = 126980;
					nText = t[(tIndex+1-4)%3+4];
					picInt = parseInt(nPicText.replace("&#","").replace(";",""));
					nPicText = "&#" + ((picInt-1-smallestPic+3)%3+smallestPic) + ";";
				}
			}else{
				//萬	索	餅
				var nNum=nText[0], newIndex = (chinesetext.indexOf(nNum)+1)%chinesetext.length;
				nText = chinesetext[ newIndex ] + nText[1];
				differenceBetweenOldAndNewIndex = newIndex - chinesetext.indexOf(nNum);

				nPicText = "&#" + (parseInt(nPicText.replace("&#","").replace(";",""))+differenceBetweenOldAndNewIndex) + ";";
			}
			list.push(new MahjongTile(nText, nType, nPicText ));
		}
		return list;
	}
}
